import pandas as pd

df = pd.DataFrame(
    {
        "语文": [4, 5, 6],
        "数学": [4, 5, 6],
        "英语": [4, 5, 6]
    },
    index=["张三", "李四", "王五"]
)

df2 = pd.DataFrame(
    {
        "语文": [4, 5, 6],
        "数学": [4, 5, 6],
        "英语": [4, 5, 6]
    },
    index=["王六", "刘启", "赵八"]
)

# 合并两个 DataFrame
df_combined = pd.concat([df, df2])

# Count number of rows with each unique value of variable
print("每个科目成绩唯一值的行数统计：")
for subject in df_combined.columns:
    print(f"{subject}:")
    print(df_combined[subject].value_counts())

# # of rows in DataFrame.
print("\n合并后 DataFrame 的行数：")
print(len(df_combined))

# Tuple of # of rows, # of columns in DataFrame.
print("\n合并后 DataFrame 的行数和列数：")
print(df_combined.shape)

# # of distinct values in a column.
print("\n合并后各科目列中的不同值的数量：")
print(df_combined.nunique())

# Basic descriptive and statistics for each column (or GroupBy).
print("\n合并后每个列的基本描述性统计信息：")
print(df_combined.describe())

# 其他汇总函数示例
print("\n合并后各列的总和：")
print(df_combined.sum())

print("\n合并后各列的非 NA/null 值数量：")
print(df_combined.count())

print("\n合并后各列的中位数：")
print(df_combined.median())

print("\n合并后各列的 25% 和 75% 分位数：")
print(df_combined.quantile([0.25, 0.75]))


# 定义一个自定义函数，这里计算平方
def square(x):
    return x ** 2


print("\n应用自定义函数（平方）到合并后的每列：")
print(df_combined.apply(square))

print("\n合并后各列的最小值：")
print(df_combined.min())

print("\n合并后各列的最大值：")
print(df_combined.max())

print("\n合并后各列的平均值：")
print(df_combined.mean())

print("\n合并后各列的方差：")
print(df_combined.var())

print("\n合并后各列的标准差：")
print(df_combined.std())
